package icontrolv2

import org.springframework.dao.DataIntegrityViolationException
import org.codehaus.groovy.grails.commons.ConfigurationHolder    //exportacion reporte

class ClienteController {
    def exportService     //exportacion reporte

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index() {
        redirect(action: "list", params: params)
    }

    def list() {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
          //exportacion reporte
        if(params?.format && params.format != "html"){
            System.out.println(params)
            response.contentType = grailsApplication.config.grails.mime.types[params.format]
            response.setHeader("Content-disposition", "attachment; filename=cliente.${params.extension}")
            List fields =["rut","dv","nombre"]

            exportService.export(params.format, response.outputStream, Cliente.list(params),[:],[:])

        }
        //exportacion reporte
        [clienteInstanceList: Cliente.list(params), clienteInstanceTotal: Cliente.count()]
    }

    def create() {
        [clienteInstance: new Cliente(params), direccionInstance: new Direccion(params)]
    }

    def save() {
        def clienteInstance = new Cliente(params)
        def direccionInstance = new Direccion(direccion: params.direccion, comuna: params.comuna)

        if (!clienteInstance.save(flush: true)) {
            direccionInstance.save()
            render(view: "create", model: [clienteInstance: clienteInstance])
            return
        }

		flash.message = message(code: 'default.created.message', args: [message(code: 'cliente.label', default: 'Cliente'), clienteInstance.id])
        redirect(action: "show", id: clienteInstance.id)
    }

    def show() {
        def clienteInstance = Cliente.get(params.id)
        if (!clienteInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'cliente.label', default: 'Cliente'), params.id])
            redirect(action: "list")
            return
        }

        [clienteInstance: clienteInstance]
    }

    def edit() {
        def clienteInstance = Cliente.get(params.id)
        if (!clienteInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'cliente.label', default: 'Cliente'), params.id])
            redirect(action: "list")
            return
        }

        [clienteInstance: clienteInstance]
    }

    def update() {
        def clienteInstance = Cliente.get(params.id)
        if (!clienteInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'cliente.label', default: 'Cliente'), params.id])
            redirect(action: "list")
            return
        }

        if (params.version) {
            def version = params.version.toLong()
            if (clienteInstance.version > version) {
                clienteInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'cliente.label', default: 'Cliente')] as Object[],
                          "Another user has updated this Cliente while you were editing")
                render(view: "edit", model: [clienteInstance: clienteInstance])
                return
            }
        }

        clienteInstance.properties = params

        if (!clienteInstance.save(flush: true)) {
            render(view: "edit", model: [clienteInstance: clienteInstance])
            return
        }

		flash.message = message(code: 'default.updated.message', args: [message(code: 'cliente.label', default: 'Cliente'), clienteInstance.id])
        redirect(action: "show", id: clienteInstance.id)
    }

    def delete() {
        def clienteInstance = Cliente.get(params.id)
        if (!clienteInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'cliente.label', default: 'Cliente'), params.id])
            redirect(action: "list")
            return
        }

        try {
            clienteInstance.delete(flush: true)
			flash.message = message(code: 'default.deleted.message', args: [message(code: 'cliente.label', default: 'Cliente'), params.id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
			flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'cliente.label', default: 'Cliente'), params.id])
            redirect(action: "show", id: params.id)
        }
    }
}
